package com.ruoyi.business.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.domain.FreightInfo;
import com.ruoyi.business.domain.bo.admin.AdminDriverSettleAccountsQueryBo;
import com.ruoyi.business.domain.vo.FreightInfoVo;
import com.ruoyi.business.domain.vo.admin.AdminCompanySettleAccountsVo;
import com.ruoyi.business.domain.vo.admin.AdminDriverSettleAccountsVo;
import com.ruoyi.business.domain.vo.admin.AdminFreightInfoVo;
import com.ruoyi.business.domain.vo.admin.AdminWaybillQueryVo;
import com.ruoyi.common.core.mapper.BaseMapperPlus;
import com.ruoyi.common.enums.SysYesNo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 运单Mapper接口
 *
 * @author ruoyi
 * @date 2024-04-20
 */
public interface FreightInfoMapper extends BaseMapperPlus<FreightInfoMapper, FreightInfo, FreightInfoVo> {

    /**
     * 更新运单紧急状态
     *
     * @param id     运单ID
     * @param name   运单名称
     * @param remark 运单备注
     * @return
     */
    default int updateUrgent(Long id, String name, String remark) {
        return update(null, new LambdaUpdateWrapper<FreightInfo>()
            .eq(FreightInfo::getFreightId, id)
            .set(FreightInfo::getIsUrgency, name)
            .set(FreightInfo::getRemark, remark)
        );
    }

    /**
     * 根据订单id统计紧急运单数量
     *
     * @param orderId
     * @return
     */
    default long countUrgentByOrderId(Long orderId) {
        return selectCount(new LambdaUpdateWrapper<FreightInfo>()
            .eq(FreightInfo::getOrderId, orderId)
            .eq(FreightInfo::getIsUrgency, SysYesNo.Y.name())
        );
    }

    /**
     * 根据订单id统计指定状态的运单数量
     *
     * @param orderId 订单id
     * @param status  运单状态
     * @return
     */
    default long countStatusByOrderId(Long orderId, String status) {
        return selectCount(new LambdaUpdateWrapper<FreightInfo>()
            .eq(FreightInfo::getOrderId, orderId)
            .eq(FreightInfo::getFreightStatus, status)
        );
    }

    /**
     * 查询运单列表
     *
     * @param page         分页对象
     * @param queryWrapper 查询条件
     * @return
     */
    IPage<AdminFreightInfoVo> selectAdminVoList(@Param("page") Page<AdminFreightInfoVo> page, @Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    /**
     * 查询运单信息
     *
     * @param queryWrapper 查询条件
     * @return
     */
    AdminWaybillQueryVo selectAdminQueryVo(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    /**
     * 根据状态统计运单数量
     *
     * @param status
     * @return
     */
    long countByStatus(@Param("status") List<String> status);

    /**
     * 根据订单id查询运单列表
     *
     * @param orderId
     * @return
     */
    default List<FreightInfo> selectByOrderId(Long orderId) {
        return selectList(new QueryWrapper<FreightInfo>()
            .lambda()
            .eq(FreightInfo::getOrderId, orderId));
    }

    /**
     * 根据司机电话统计运单数量
     *
     * @param driverTel
     * @param status
     * @return
     */
    default long countByDriverTel(String driverTel, List<String> status) {
        return selectCount(new QueryWrapper<FreightInfo>()
            .lambda()
            .eq(FreightInfo::getDriverTel, driverTel)
            .in(FreightInfo::getFreightStatus, status));
    }

    /**
     * 修改运单状态
     *
     * @param id     运单ID
     * @param status
     * @return
     */
    default int updateStatus(Long id, String status) {
        return update(new LambdaUpdateWrapper<FreightInfo>()
            .eq(FreightInfo::getFreightId, id)
            .set(FreightInfo::getFreightStatus, status)
        );
    }

    IPage<AdminDriverSettleAccountsVo> selectDriverSettleAccountsList(@Param("page") Page<AdminDriverSettleAccountsVo> page, @Param("queryBo") AdminDriverSettleAccountsQueryBo queryBo);
}
